/*
 * @Author: your name
 * @Date: 2021-06-20 18:20:01
 * @LastEditTime: 2021-06-20 22:22:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hbs\libs\push\sms.ts
 */

const tencentcloud = require("tencentcloud-sdk-nodejs");
const SmsClient = tencentcloud.sms.v20210111.Client;
/**
 * @description: 用于对阿里发送短信验证码的处理
 * @param {*}
 * @return {*}
 */
const smsClient = tencentcloud.sms.v20210111.Client
export default class SmS {
    /**
     * @description:初始化客户端,需要传入两个参数 
     * @param {string} accessKeyId
     * @param {string} accessKeySecret
     * @return {*}
     */
    private static createClient(accessKeyId: string, accessKeySecret: string): any {
        const clientConfig = {
            credential: {
                secretId: accessKeyId,
                secretKey: accessKeySecret,
            },
            region: "ap-guangzhou",
            profile: {
                httpProfile: {
                    endpoint: "sms.tencentcloudapi.com",
                },
            },
        };
        const client = new SmsClient(clientConfig);
        return client;
    }
    /**
     * @description: 发送短信验证码函数
     * @param {string} accessKeyId
     * @param {string} accessKeySecret
     * @param {SmsTemp} SmsTemp
     * @return {*}
     */
    static async sendSmsRequest(accessKeyId: string, accessKeySecret: string, SmsTemp: SmsTemp): Promise<any> {
        let client = SmS.createClient(accessKeyId, accessKeySecret);
        return new Promise((resolve, reject) => {
            // 通过client对象调用想要访问的接口，需要传入请求对象以及响应回调函数
            client.SendSms(SmsTemp, function (err, response) {
                // 请求异常返回，打印异常信息
                if (err) {
                    reject(err);
                    console.log(err);
                    return
                }
                // 请求正常返回，打印response对象
                resolve(response)
            });
        });

    }
    /**
     * @description: 生成短信验证码模型类
     * @param {string} phoneNumbers
     * @param {string} signName
     * @param {string} templateCode
     * @param {string} templateParam
     * @return {*}
     */
    static createSmsTemp(phoneNumbers: any, signName: string, templateCode: string, templateParam: Array<string>) {
        return new SmsTemp(phoneNumbers, signName, templateCode, templateParam);
    }


}
class SmsTemp {
    SignName: string;
    TemplateId: string;
    SmsSdkAppId: string;
    TemplateParamSet: Array<string>;
    PhoneNumberSet: Array<string>;
    constructor(phoneNumbers: any, signName: string, templateCode: string, templateParam: Array<string>, SmsSdkAppId: string = '1400660986') {
        if(typeof(phoneNumbers)=="string"){
            this.PhoneNumberSet = [phoneNumbers];
        }else{
            this.PhoneNumberSet=phoneNumbers;
        }
      
        this.SignName = signName;
        this.TemplateId = templateCode;
        this.TemplateParamSet = templateParam;
        this.SmsSdkAppId=SmsSdkAppId;
    }

}

